<?php

namespace app\wap\controller;


use app\admin\model\Active;
use app\admin\model\Area;
use app\admin\model\Notice;
use app\admin\model\Schedu;
use app\admin\model\Stoptell;
use app\admin\model\Users;
use app\admin\model\Yactive;
use app\home\model\Content;
use think\Controller;
use think\Cookie;
use think\Session;
use think\Captcha;
use think\Db;
use think\Config;
use think\Request;
use \think\Validate;

use Endroid\QrCode\QrCode;
use app\home\model\Visiting;
class User extends Controller
{
    public function __construct()
    {

        parent::__construct();

        $atime = strtotime(date("Y-m-d 16:00")); //当日16:00
        (time()>$atime)?$a=2:$a=1; //超过当日16:00 延后一天
        for ($i=$a;$i<=$a+6;$i++){ //预约7天内的时间
            $time[]= date("Y-m-d",strtotime(date('Y-m-d')."+$i day"));
        }
        $this->assign('time',$time);//时间段
        $this->assign("position_type",config("position_type"));
        $this->assign('visit_time',config('visit_time2')); //日期
        $this->assign('position_type',config('position_type')); //职业类型
    }

    //登录
    public function home_login(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $verify_code = $this->request->param('verify_code');

            $userModel = new \app\home\model\User();
            $res = $userModel->login($data);
            if($res['code'] == 1){

                session('user',$res['res']);
                session('uid',$res['res']['user_id']);

            }
            exit(json_encode($res));
        }
        return $this -> fetch();
    }

    public function login(){
        $data = input("get.");
        $userModel = new \app\home\model\User();
        $result = $userModel->login($data);
        if($result['code'] == 1){
//            dd($result);
            if($result['res']['status'] == 0){
//                echo "<script> alert('账号被禁止');location.href = 'wap_index';</script>";exit;
                $this->error('账号被禁止','/wap_index');
            }
            session('mobile',$result['res']['mobile']);
            session('uid',$result['res']['user_id']);
        }else{
            $this->error('用户不存在','/wap_index');

//            echo "<script> alert('用户不存在');location.href = 'wap_index';</script>";exit;
        }
        if($data['styles']==1){
            echo "<script>location.href = '/wap/user/rules';</script>";exit;
        }else{
            echo "<script>location.href = 'orders?mobile=".$data['mobile']."';</script>";exit;

        }
    }
    //预约须知
    public function rules(){
        $result = Notice::where("id",1)->find();
        $this->assign("result",$result);
        return $this -> fetch();
    }

    //注册
    public function register(){
           $data = array_filter(input("get."));

        if(cache("register_code") !=$data['code']){
            echo "<script> alert('验证码不正确');location.href = 'wap_index';</script>";exit;
        }
        if(cache("register_code")==false){
            echo "<script> alert('验证码不存在');history.go(-1);</script>";exit;
        }
//        unset($data['code']);
            $userModel = new \app\home\model\User();
            if (!preg_match('/(^(13\d|15[^4\D]|17[13678]|18\d)\d{8}|170[^346\D]\d{7})$/', $data['mobile'])) {
                $this->error('请输入正确的手机号!');
            }


            $findUserCount = Visiting::where("mobile",$data['mobile'])->select();
            $active = Yactive::where("mobile",$data['mobile'])->select();
        if($findUserCount){
            session("mobile",$data['mobile']);
            echo "<script>location.href = 'orders?mobile=".$data['mobile']."';</script>";exit;
        }elseif ($active){
            session("mobile",$data['mobile']);
            echo "<script>location.href = 'my_active?mobile=".$data['mobile']."';</script>";exit;
        }
        else{
            echo "<script> alert('没有此人预约信息');location.href = 'wap_index';</script>";exit;
        }
//
    }

    //首页
    public function index(){
        $model=new Content();
        $list=$model->where("status",1)->field("content")->order("id desc")->select();  //获取公告信息
//        $res = Db::name('users')->where('openid','=',session("open_id"))->find();
//        if($res){
//            session("mobile",$res['mobile']);
//        }
        $res = Active::where("switch",1)->select();
//        dd($res);
            $res = $res ? 1 : 0;
//            dd($res);
        $this->assign("list",$list);
        $this->assign("res",$res);
        return $this -> fetch();
    }


    //用户填写预约信息
    public function orderInfo(){
        if(request()->isPost()){
            $data = array_filter(input("post."));
//            dd($data);
            $cache_key = 'ww_'.$data['mobile'];
            if(cache($cache_key)){
                $this->error('请勿频繁提交');
//                echo "<script> alert('请勿频繁提交');history.go(-1);</script>";exit;
            }

            if($data['venue']!="C"){
                unset($data['day1']);
            }
////            if(isset($data['views']) <= 5 ){
////                echo "<script> alert('添加人数不能少于5人');history.go(-1);</script>";exit;
////            }
            if(isset($data['views']) > 100){
                $this->error('添加人数不能超过100人');
//                echo "<script> alert('添加人数不能超过100人');history.go(-1);</script>";exit;
            }
            if(!isset($data['type'])){
                unset($data['admin_id']);
            }
            if(cache("yuyue_code")==false){
                $this->error('验证码不存在或已失效');
//                echo "<script> alert('验证码不存在');history.go(-1);</script>";exit;
            }
            if(cache("yuyue_code") !=$data['code']){
                $this->error('验证码错误');
//                echo "<script> alert('验证码不正确');history.go(-1);</script>";exit;
            }

            $stopyuyue = Stoptell::where("mobile",$data['mobile'])->find();
            if($stopyuyue){
                echo "<script>history.go(-1);</script>";exit;
            }
            $data['code'] =   str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
            $add = new Visiting();
            $time = config("visit_time2");
            if($data['venue']=="C"){
                $data['venue'] = 'A';
                if(!isset($data['explain'])){
                    foreach($time as $key =>$value){
                        if($data['time'] == $value){
                            $data['between_time']=$key;
                        }
                    }
                }

                if(isset($data["arriveb_part"])){
                    $data['dinner'] = $data['arriveb_part']=='上午'?0:1;
                    $data['arrival_time'] = $data['arriveb'];
                }

                if(isset($data['explain'])){
                    $stop = objToArray($add->selectColumn($data['day'],$data['venue'],$data['explain']));
                    if(in_array($data['dinner'],$stop)){
                        $this->error('预约时间已满，请选择其他时间');

//                        echo "<script> alert('预约时间已满，请选择其他时间');history.go(-1);</script>";exit;
                    }
                }

                if(isset($data["arrivea_part"])){
                    $data['dinner'] = $data['arrivea_part']=='上午'?0:1;
                    $data['arrival_time'] = $data['arrivea'];
                }

                if(isset($data['explain'])){
                    $stop = objToArray($add->selectColumn($data['day'],$data['venue'],$data['explain']));
                    if(in_array($data['dinner'],$stop)){
                        $this->error('预约时间已满，请选择其他时间');

//                        echo "<script> alert('预约时间已满，请选择其他时间');history.go(-1);</script>";exit;
                    }
                }

                if(isset($data['explain'])){
                sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['arrival_time']."     北山街馆区，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。地址：北山街84号。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }else{
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['time']."     北山街馆区，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。地址：北山街84号。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }
                $result = $add->addeditVisit($data);
                $data['venue'] = 'B';

                if(!isset($data['explain'])){
                    foreach($time as $key =>$value){
                        if($data['time1'] == $value){
                            $data['between_time']=$key;
                        }
                    }
                }


                if(isset($data["arriveb_part"])){
                        $data['dinner'] = $data['arriveb_part']=='上午'?0:1;
                        $data['arrival_time'] = $data['arriveb'];
                }

                $data['day']=$data['day1'];
                $data['code'] =   str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);

                if(isset($data['explain'])){
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['arrival_time']."     栖霞岭馆区，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。地址：栖霞岭54号。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }else{
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['time']."     栖霞岭馆区，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。地址：栖霞岭54号。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }

                $result1 = $add->addeditVisit($data);
            }else{
                if(!isset($data['explain'])){
                    foreach($time as $key =>$value){
                        if($data['time'] == $value){
                            $data['between_time']=$key;
                        }
                    }
                }
//                dd(1);
               $guan = $data['venue']=='A'?'北山街馆区，地址是北山街84号':'栖霞岭馆区，地址是栖霞岭54号';


                if(isset($data["arrivea_part"])||isset($data["arriveb_part"])){
                    if(isset($data['arrivea_part'])) {
                        $data['dinner'] = $data['arrivea_part']=='上午'?0:1;
                    }else{
                        $data['dinner'] = $data['arriveb_part']=='上午'?0:1;
                    }
                        if(isset($data['arrivea'])){
                        $data['arrival_time'] = $data['arrivea'];
                    }else{
                        $data['arrival_time'] = $data['arriveb'];
                    }
                 }

                if(isset($data['explain'])){
                    $stop = objToArray($add->selectColumn($data['day'],$data['venue'],$data['explain']));
                    if(in_array($data['dinner'],$stop)){
                        $this->error('预约时间已满，请选择其他时间');

//                        echo "<script> alert('预约时间已满，请选择其他时间');history.go(-1);</script>";exit;
                    }
                }
                if(isset($data['explain'])){
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['arrival_time'].$guan."  ，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }else{
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['time'] .$guan."   ，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }
                $result = $add->addeditVisit($data);
            }
            session("mobile",$data['mobile']);

            cache($cache_key,1,5);
            cache("yuyue_code",0);//成功后验证码直接失效

            $this->success('预约成功','/orders?mobile='. $data['mobile']);

//            echo "<script> alert('预约成功');window.location.href = '/orders?mobile=". $data['mobile']."';</script>";exit;
        }


        $result = new  \app\home\model\User();
        $province = Area::where("pid",0)->column("id,name");
        $yuyue = new Visiting();
        $atime = strtotime(date("Y-m-d 16:00")); //当日16:00
        (time()>$atime)?$a=2:$a=1; //超过当日16:00 延后一天
        for ($i=$a;$i<=$a+6;$i++){ //预约7天内的时间
            $time[]= date("Y-m-d",strtotime(date('Y-m-d')."+$i day"));
        }
        $yuyue = $yuyue->selectColumn($time[0],"A",0);
        $city = Area::where("pid",933)->column("id,name");
        $this->assign('yuyue',$yuyue);//时间段
        $this->assign("province",$province);
        $this->assign("city",$city);
        $this->assign("list" ,$list = $result->findAll(session("uid")));
        return $this -> fetch();
    }

    //用户预约成功回执
    public function orderSuccess(){
        $data = input("get.");
        $yuyue = new Visiting();
        //dump($yulist);die();
//         dd($yuyue->SelectAll($data['id']));
        $result = $yuyue->SelectAll($data['id']);

            if($result['explain']==0){
                $between = config("visit_time2")[$result['between_time']];
                $time = substr($between,0,strrpos($between,"-"));
                $result['time1'] = strtotime(date($result['day']."14:00"))-86400;
                $result['can']   = time()>=$result['time1']?1:0;
            }else{
                $result['time1'] = strtotime(date($result['day'].$result['arrival_time']))-86400;
                $result['can']   = time()>=$result['time1']?1:0;
            }
//        dd(1);
//        dd(objToArray($result));
        $this->assign(["list"=>$result
        ]);

        return $this -> fetch();
    }

    //我的预约
    public function orders(){

        $data = input("get.");
        $order_list = new Visiting();
        $this->assign('visit_list',$order_list->geren($data['mobile']));
        return $this -> fetch();

    }

    //预约订单详情
    public function orderDetail(){


        if(request()->isPost()){
            $data = input("post.");

            $add = new Visiting();
            $time = config("visit_time2");

            if($data['explain']==1){
                if(isset($data["arrive_part"])){
                        $data['dinner'] = $data['arrive_part']=='上午'?0:1;
                    if(isset($data['arrivea'])){
                        $data['arrival_time'] = $data['arrive'];
                    }else{
                        $data['arrival_time'] = $data['arrive'];
                    }
                }
//                dump(1);
            }else{
                foreach($time as $key =>$value){
                    if($data['time'] == $value){
                        $data['between_time']=$key;
//                        dump($key);
                    }
                }
//                dump($key);
            }
//            if(isset($data['explain'])){
//                $stop = objToArray($add->selectColumn($data['day'],$data['venue'],$data['explain']));
//                if(in_array($data['dinner'],$stop)){
//                    echo "<script> alert('预约时间已满，请选择其他时间');history.go(-1);</script>";exit;
//                }
//            }
//            dump($data['between_time']);
//            dd($data);
            $result = $add->editVisit($data);
            $this->success('预约修改成功', '/orders?mobile='. session("mobile"));
//            echo "<script> alert('预约修改成功');window.location.href = '/orders?mobile=". session("mobile")."';</script>";exit;
        }
        $data = input("get.");
//        dd($data);
        $yuyue = new Visiting();
        $yulist = $yuyue->selectColumn($data['day'],$data['venue'],$data['explain']);
        $data['venue']=($data['venue'] == 'A')?1:2;
        if($data['explain'] == 0){
            $data['between_time'] =  $yuyue->SelectAll($data['id'])['between_time'];
            $visit_time=config("visit_time2");
            $list = $yuyue->SelectAll($data['id']);
        }else{
            $visit_time=config("visit_time2");
            $list = $yuyue->SelectAll($data['id']);
            $list['arrival_time1'] = substr($list['arrival_time'],-2);
            $list['arrival_time'] = substr($list['arrival_time'],0,strrpos($list['arrival_time'],":"));
        }
//        dd( $visit_time);
        $this->assign(["yuyue"=>$yulist, "list"=>$list, "data"=>$data
        ,"visit_time"=>$visit_time]);
        return $this -> fetch();
    }

    //发表评论
    public function comment(){
        $yuyue = new Visiting();
        if(request()->isPost()){
            $data = array_filter(input("post."));
            if(isset($data['goodComment'])){
                $data['evcontent'] =$data['evcontent'] ? $data['goodComment'].",".$data['evcontent']:$data['goodComment'];
            }
            $result = $yuyue->editVisit($data);
            $this->success('修改成功','orders?mobile='. session("mobile"));
//            echo "<script> alert('修改成功');window.location.href = 'orders?mobile=". session("mobile")."';</script>";exit;
        }
        $data = array_filter(input("get."));
//        dd($data);
        $this->assign("id",$data['id']);
        return $this -> fetch();
    }

    public function getQrcode(){
        $qrCode = new QrCode('http://www.baidu.com');
        $qrCode->setSize(200);
        header('Content-Type: '.$qrCode->getContentType());
        echo $qrCode->writeString();
        exit;

        //带logo的
//        $set_log =true;
//        $qrCode = new QrCode('http://baidu.com');
//        if($set_log ==true){
//            $qrCode->setLogoPath('thumb/link.jpg');
//            $qrCode->setLogoWidth(90);
//        }
//        $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH);
//        $name = rand(1,99999999).time();
//        $path ='static/qrcode/'.$name.'.png';
//        $qrCode->writeFile($path);
//        header('Content-Type: '.$qrCode->getContentType());
//        echo $qrCode->writeString(); //@去操作这张图片
//        // //@删除文件
//        unlink($path);
//        exit;
    }
    /**
        * 动态获得的信息
     */
    public function runimformation(){
        $data1 = input("post.");
        $yuyue = new Visiting();
        $visit_time = config('visit_time2') ;
        $visit_time2= config('visit_time')  ;
        $data=array();

        if($data1['venue']=='C'){
            $yuyuea = objToArray($yuyue->selectColumn($data1['day'],'A',$data1['explain']));
            $yuyueb = objToArray($yuyue->selectColumn($data1['day1'],'B',$data1['explain']));
            $data['venue'] = '北山街馆区';
            $data['venue1'] = '栖霞岭馆区';
            $data['yuyue']=array_values($yuyuea);
            $data['yuyueb']=array_values($yuyueb);
        }else{
            unset($data['day1']);
            $yuyue = objToArray($yuyue->selectColumn($data1['day'],$data1['venue'],$data1['explain']));
            $data['yuyue']=array_values($yuyue);
            $data['venue'] = $data1['venue']=='A'?'北山街馆区':'栖霞岭馆区';
        }
        $data['yuyuea'] = array();
        $data['yuyuebb']= array();
        if($data1['explain'] ==1 ){
            $data['yuyue'] = implode(",",$data['yuyue']);
            if(isset($data['yuyueb'])){
                $data['yuyueb'] = implode(",",$data['yuyueb']);
            }
        }else{
            $data['visit']=array_values($visit_time);
        }
            $data["explain"]  = $data1['explain'] ;
//        dd($data);
        return $data;
    }

    /**
        * 取消预约
     */
    public function delYuyue(){
        $data=input("post.");
//        dd($data);
        $del = new Visiting();
        $result = $del->delYuyue($data);
        return info($result['msg'],$result['code']);
    }


    /**
     * 获取调解员
     */
    public function gettiaojie(){
        $data = array_filter(input("get."));
        $this->time = config("visit_time2");
        foreach($this->time as $key =>$value){
            if($data['between_time'] == $value){
                $data['between_time']=$key;
            }
        }
        $result =Schedu::where($data)->with('users')->select();
        return info('操作成功！', 1,'',$result);

    }
    /**
     * 个人信息
     */

    public function personal(){
        $result = new Users();
        if(\request()->isPost()){
            $data = input("post.");
            $person= $result::where("id",$data['id'])->Update($data);
            $this->success('修改成功','orders?mobile='. session("mobile"));
//            echo "<script> alert('修改成功');window.location.href = 'orders?mobile=". session("mobile")."';</script>";
        }
        $data = session("mobile");
        $person= $result::where("mobile",$data)->find();
        $this->assign("person",$person);
        return $this -> fetch();
    }


    //清楚session
    public function tuichu(){
        \think\Session::delete("mobile");
        return $this->redirect("wap/user/index");
    }
    /**
     * 市查询
     */
    public function province(){
        $data = input("get.");
        $data = Area::where("pid",$data['id'])->column("id,name");
        return $data;
    }

    /**
     * 区查询
     */
    public function city(){
        $data = input("get.");
        $data = Area::where("pid",$data['id'])->column("id,name");
        return $data;
    }

    /**
     * 短信注册
     */
    public function register_code(){
        $data = input("get.");
        $code =  str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
        cache("register_code",$code,60);
        $result = sendSMS("【五四宪法馆】您好,登录的验证码为".$code,$code,$data['mobile']);
        return "发送成功";
    }


    /**
     * 短信预约
     */
    public function yuyue_code(){
        $data = input("get.");
        $code =  str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
        cache("yuyue_code",$code,120);
        $stopyuyue = Stoptell::where("mobile",$data['mobile'])->find();
        if($stopyuyue){
            echo "<script>history.go(-1);</script>";exit;
        }
        $result = sendSMS("【五四宪法馆】您好,预约的验证码为".$code,$code,$data['mobile']);
        return "发送成功";
    }


    /**
     * 时间预约
     */
    public function yuyue_time(){
        $data = input("get.");
        $code =  str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
        cache("yuyue_code",$code,120);
        $result = sendSMS("【五四宪法馆】您好,预约的验证码为".$code,$code,$data['mobile']);
        return $result['data'];
    }
//
    /**
     * 解除禁止预约时间
     */
    public function stop(){
        $result = Stoptell::where("stop_time" ,"<=",time())->select();
        foreach($result as $k=>$value){
            Stoptell::where("id" ,$value['id'])->delete();
        }
        echo "禁止解除成功";
    }
    /**
     * 预约前一天发送验证码
     */
    public function send_code(){
        $result = Visiting::where("status",1)->where('send_status',0)->where("day",date("Y-m-d",time()))->select();
//        dump(objToArray($result));
//        dd(objToArray($result));
        foreach($result as $k =>$value){
            $between = config("visit_time2")[$value['between_time']];
            $time = substr($between,0,strrpos($between,"-"));
            $result[$k]['time'] = strtotime(date($value['day'].$time))-86400;
            $result[$k]['dq'] = time() >= $result[$k]['time'] ? 1 : 0;
            $guan = $value['venue']=='A'?'北山街馆区，地址是北山街84号':'栖霞岭馆区，地址是栖霞岭54号';
            if($result[$k]['dq']==1){
                Visiting::where("id",$value['id'])->update(['send_status'=>1]);
                if($value['explain']==0){
                    sendSMS("【五四宪法历史馆】您已预约".$value['day']."    ".$between .$guan."，预约码：".$value['code']."。请您携带有效身份证件按时签到参观。团队参观请携带介绍信。取消或改期截止时间为今日14点前。点击".\request()->domain()."查看您的预约信息。".$value['day'].$between."还有一天将要开始",$value['code'],$value['mobile']);
                }else{
                    sendSMS("【五四宪法历史馆】您已预约".$value['day']."    ".$value['arrival_time'] .$guan."，预约码：".$value['code']."。请您携带有效身份证件按时签到参观。团队参观请携带介绍信。取消或改期截止时间为今日14点前。点击".\request()->domain()."查看您的预约信息。".$value['day'].$between."还有一天将要开始",$value['code'],$value['mobile']);

                }
           }
        }
//        dd(objToArray($result));
        echo "发送验证码成功";
    }


    /**
     * 活动自动判断是否参观
     */
    public function active_visiting(){
        set_time_limit(0);
        $result = Yactive::where("status",1)->where("day",date("Y-m-d",time()))->with("active1")->select();
//        dump(objToArray($result));
        foreach($result as $k =>$value){
            $time = strtotime(date($value['day']."18:00"));
            if(time()>= $time){
                $between = config("visit_time2")[$value['active1']['timer']];
                $time = substr($between,0,strrpos($between,"-"));
                $result[$k]['time'] = strtotime(date($value['day'].$time));
                $result[$k]['dq'] = time() >= $result[$k]['time'] ? 1 : 0;
                if($value['active_views']==""){
                    Yactive::where("id",$value['id'])->update(['status'=>2]);
                }
            }
        }
        echo "活动自动判断成功";
    }
    /**
     * 预约自动判断是否参观
     */
    public function send_visiting(){
        set_time_limit(0);
        $result = Visiting::where("status",1)->where("day",date("Y-m-d",time()))->select();
        foreach($result as $k =>$value){
            $time = strtotime(date($value['day']."18:00"));
            if($value['explain']==1){
                if(time()>= $time){
                    if($value['actual_views']==""){
                        Visiting::where("id",$value['id'])->update(['status'=>6]);
                        $res = Stoptell::where('mobile',$value['mobile'])->find();
                        if($res){

                        }else{
                            Stoptell::insert(['mobile'=>$value['mobile'],'stop_time'=>time()+(86400*30)]);
                        }
                    }else{
                        Visiting::where("id",$value['id'])->update(['status'=>4]);
                    }
                }
            }else{
                Visiting::where("id",$value['id'])->update(['status'=>4]);
            }

        }
        echo "自动判断成功";
    }


    /**
     * 活动列表查询
     */
    public function activity(){
        $result  = Active::where("switch",1)->order("day desc")->select();
        foreach($result as $k =>$val) {
            $result[$k]['end']   = strtotime($val['endday']) >= time() ? 0 : 1;
            $result[$k]['enday'] = ceil(round((strtotime($val['endday']) - time()) / 86400, 1));
            $result[$k]['timer'] = config("visit_time2")[$val['timer']];
        }
        $this->assign("active",$result);
        return view();
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查询单个预约
     */
    public function activity_detail(){
        $data = input("get.");
        $result  =  Active::where("switch",1)->where('id',$data['id'])->find();
        $result['end']   = strtotime($result['endday']) >= time() ? 0 : 1;
        $result['enday'] = ceil(round((strtotime($result['endday'])-time())/86400,1));
        $result['timer'] = config("visit_time2")[$result['timer']];
        $this->assign("active",$result);
        return view();
    }
    /**
     * 预约活动
     */
    public function activity_order(){

        if(request()->isPost()) {
            $data   = array_filter(input("post."));
//            dd($data);
           $code =  str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
            $cache_key = 'ww_'.$data['mobile'];
            if(cache($cache_key)){
//                echo "<script> alert('请勿频繁提交');history.go(-1);</script>";exit;
                $this->error('请勿频繁提交');
            }
            if(cache("yuyue_code") !=$data['code']){
                $this->error('验证码不正确');
//                echo "<script> alert('验证码不正确');history.go(-1);</script>";exit;
            }
            if(cache("yuyue_code")==false){
                $this->error('验证码不存在');

//                echo "<script> alert('验证码不存在');history.go(-1);</script>";exit;
            }
            $stopyuyue = Stoptell::where("mobile",$data['mobile'])->find();
            if($stopyuyue){
                echo "<script>history.go(-1);</script>";exit;
            }
            unset($data['code']);
            $active = Active::where("switch", 1)->where('id', $data['id'])->find();
            if($data['views'] > $active['views']){
                $this->error('添加人数不能超过{$active[views]}');
//                echo "<script> alert('添加人数不能超过{$active['views']}');history.go(-1);</script>";exit;
            }
            $sum   = Yactive::where("active_id", $data['id'])->where("status",1)->where("day", $data['day'])->sum("views");
            if($sum){
                if ($data['views'] + $sum > $active['views']) {
                    $this->error('当前时段已人数超出,请预约其他时间');

//                    echo "<script> alert('当前时段已人数超出,请预约其他时间');history.go(-1);</script>";exit;
                }
            }
            $data['active_id']=$data['id'];
            unset($data['id']);
            $data['code'] = $code;
            session("mobile",$data['mobile']);
            $data['status'] = 1;
            Yactive::create($data);

            cache($cache_key,1,5);
            sendSMS("【五四宪法馆】预约成功。活动预约码：".$code."。",$code,$data['mobile']);
//            echo "<script> alert('预约成功');window.location.href = '/my_active?mobile=". $data['mobile']."';</script>";exit;
            $this->success('预约成功','/my_active?mobile='. $data['mobile']);
        }
        $data = input("get.");
        $active = Active::where("switch", 1)->where('id', $data['id'])->find();
        $active['timer'] = config("visit_time2")[$active['timer']];
        $result['enday'] = ceil(round((strtotime($active['endday'])-strtotime($active['day']))/86400,1));
         for ($i=0;$i<=$result['enday'];$i++){ //预约7天内的时间
             if(strtotime($active['day']. "+".$i."day")<=time()){
             }else{
                 $time[]= date("Y-m-d",strtotime($active['day']. "+".$i."day"));
             }
        }
        $sum = Yactive::where(["active_id"=>$data['id'],"status"=>1])->where("day",$time[0])->sum("views");
        if ($sum >= $active['views']) {
            $active['sum'] = 1;
        }else{
            $active['sum'] = $active['views']-$sum;
        }
//        dd($active);
        $province = Area::where("pid",0)->column("id,name");
        $this->assign("province",$province);
        $this->assign("active",$active);
        $this->assign("time",$time);
        return view();
    }

    /**
     * 我的活动列表
     */
    public function my_active(){
        $data = array_filter(input("get."));
        $result = Yactive::where("mobile", $data['mobile'])->with("active")->order("id desc")->select();
        foreach($result as $k=>$val){
            $result[$k]['active']['timer']=config("visit_time2")[$val['active']['timer']];
        }
        $this->assign("active",$result);
        return view();
    }
    /**
     * 活动人数
     */
    public function persionnumber(){
        $data = array_filter(input("get."));
        $active = Active::where("switch", 1)->where('id', $data['id'])->find();
        $sum = Yactive::where("active_id", $data['id'])->where("day",$data['day'])->sum("views");
        if ($sum >= $active['views']) {
            $active['sum'] = 1;
        }else{
            $active['sum'] = $active['views']-$sum;
        }
       return $active['sum'];
    }


    /**
     * 单个活动
     */
    public function active_find(){
        $data = array_filter(input("get."));
//        dd($data);
        $result = Yactive::where("id", $data['id'])->with('active1')->find();
        $result['active']['timer'] = config("visit_time2")[ $result['active']['timer'] ];
        $result['del'] = strtotime($result['day'])-86400 <= time() ?1 :0;
        $this->assign("active",$result);
        return view();
    }

    /**
     * 取消活动
     */
    public function del_active(){
        $data = array_filter(input("post."));
//        dd($data);
        $result = Yactive::where("id", $data['id'])->update(['status'=>0]);
        return info("取消成功",1);
    }
//
//    /**
//     * 活动预约填写信息
//     */
//    public function activity_order(){
//        return view();
//    }

}